﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace HTTPServer
{

    public enum StatusCode
    {
        OK = 200,
        InternalServerError = 500,
        NotFound = 404,
        BadRequest = 400,
        Redirect = 301
    }

    class Response
    {
        string responseString;
        public string ResponseString
        {
            get
            {
                return responseString;
            }
        }
        StatusCode code;
        List<string> headerLines = new List<string>();
        public Response(StatusCode code, string contentType, string content, string redirectoinPath)
        {
            //TODO: Create this.ResponseString
            headerLines.Add("Content-Type: " + contentType);
            headerLines.Add("Content-Length: " + content.Length.ToString());
            headerLines.Add("Date: " + DateTime.Now.ToString());
            if (redirectoinPath != null)
            {
                headerLines.Add("Location: " + redirectoinPath);
            }
            this.code = code;

            // the request string
            this.responseString = string.Empty;
            this.responseString += this.GetStatusLine(code);
            if (headerLines != null)
            {
                foreach (string line in headerLines)
                {
                    this.responseString += line + "\r\n";
                }
            }

            this.responseString += "\r\n";
            this.responseString += content;
        }

        private string GetStatusLine(StatusCode code)
        {
            string statusLine = "";
            statusLine += "HTTP/1.1 ";
            statusLine += ((int)code).ToString();
            statusLine += " ";
            statusLine += code.ToString();
            statusLine += "\r\n";
            return statusLine;
        }
    }
}
